﻿$(function(){//延迟加载  
	var ipresId='';
	var ipdeployId='';
	var oltip='';
	var oltname='';
	var oltponnum=0;
	var ylpon=0;
	var boardnum=0;
	var dvfirm='';
	var oltId='';
	var pppvlanbegin='';
	var pppvlanend='';
	var olttype='';
	var zhukong='';
	var voipvlan='';
	var iptvvlan='';
	var ylvlan='';
	var voiprange='';
	var voipgw='';
	var oltvoip='';
	var iprange='';
	var getgw='';
	var noltId=$('#noltId').val();

	$('#oltname').textbox({
		prompt:'为空将会自动生成',
	});
	$('#oltponnum').numberbox({
		required:true
	});
	$('#ylpon').numberbox({
		prompt:'预留..',
		min:2,
		max:10,
		required:true
	});
	$('#boardnum').numberbox({
		required:true
	});

	$('#pppvlanbegin').numberbox({
		prompt:'PPPOE VLAN起始...',
		required:true,
		buttonText:'生成',
		buttonIcon:'icon-search',
		onClickButton:function(){
			if($(this).val()==''){
				alert('不合法');
			}else{
				pppvlanbegin=$('#pppvlanbegin').val();
				oltponnum=$('#oltponnum').textbox('getValue');
				$.ajax({
					url:'getoltvlans?oltpppvlan='+($('#pppvlanbegin').val()+'-'+$(this).val()),
					success:function(data){
						$('#iptvvlan').textbox('setValue',data.iptvvlan);
						$('#ylvlan').textbox('setValue',data.ylvlan);
						$('#voipvlan').textbox('setValue',data.voipvlan);
						$('#pppvlanend').textbox('setValue',Number(pppvlanbegin)+Number(oltponnum)/4-1);
					}
				});
			}
		}
	});
	$('#advlan').numberbox({
		required:true
	});
	$('#olttype').combobox();
	$('#oltpontype').combobox();
	
	$('#xhrow').load('dvfirmsec',function(){
		$.getScript('/xasj/js/component/dvmodelsec.js');
		$('#dvmodel').combobox({
			onChange:function(newValue,oldValue){
				if(newValue=='')return;
				$.ajax({
					url:'getmodelset?dvmodelId='+newValue,
					success:function(data){
						$('#oltponnum').numberbox('setValue',data.portnum);
						$('#boardnum').numberbox('setValue',data.boardnum);
						$('#ylpon').numberbox('setValue',parseInt(data.ylport));
						$('#zhukong').textbox('setValue',data.zhukong);
					}
				})
			}
		})
	});
	
	$('#jfrow').load('showjifangsec',function(){
		$.getScript('/xasj/js/component/jifangsec.js');
	});
	
	$('#voipnum').numberbox({
		prompt:'请求数量',
		min:3,
		value:4,
		//max:20,
		required:true
	});
	
	$('#zhukong').textbox({
		//max:20,
		required:true
	});
	
	$('#ceshi').click(function(){
		alert($('#jifang').combobox('getValue'));
	});
	
	$('#requestipnum').numberbox({
		prompt:'请求数量',
		value:4,
		min:3,
		//max:20,
		required:true
	});
	
	$('#getoltip').textbox({
		buttonText:'获取IP',
		buttonIcon:'icon-search',
		prompt:'自动获取...',
		onClickButton:function(){
			if($('#jifang').combobox('getValue')=='请选择'||$('#requestipnum').val()==''){
				alert('请选择机房和需求的IP数')
			}else{
				//alert($('#jifang').combobox('getValue')+$('#requestipnum').val());
				var url='requestip?jfId='+$('#jifang').combobox('getValue')
				+'&requestipnum='+$('#requestipnum').val()+'&dvbelong=oltip'+'&djxId='
				+$('#dajuxiang').combobox('getValue');
				//alert(url);
				$.ajax({					
					url:'requestip?jfId='+$('#jifang').combobox('getValue')
					+'&requestipnum='+$('#requestipnum').val()+'&dvbelong=oltip'+'&djxId='
					+$('#dajuxiang').combobox('getValue'),
					type:'get',
					dataType:'json',
					success:function(data){
						//alert(data.iprange);
						ipdeployId=data.ipdeployId;
						ipresId=data.ipresId;
						oltip=$('#oltip').val();
						$('#getoltip').textbox('setValue',data.dvip);
						$('#getgw').textbox('setValue',data.dvgateway);
						$('#iprange').textbox('setValue',data.iprange);
						alert(ipresId);
					}
				})
			}
		}
	});

	$('#getoltvoip').textbox({
		buttonText:'获取IP',
		buttonIcon:'icon-search',
		prompt:'自动获取...',
		onClickButton:function(){
			if($('#jifang').combobox('getValue')=='请选择'||$('#voipnum').val()==''){
				alert('请选择机房和需求的IP数')
			}else{
				//alert($('#jifang').combobox('getValue')+$('#requestipnum').val());
				var url='requestip?jfId='+$('#jifang').combobox('getValue')
				+'&requestipnum='+$('#voipnum').val()+'&dvbelong=oltvoip'+'&djxId='
				+$('#dajuxiang').combobox('getValue');
				//alert(url);
				$.ajax({					
					url:url,
					type:'get',
					dataType:'json',
					success:function(data){
						//alert(data.iprange);
						ipdeployId=data.ipdeployId;
						ipresId=data.ipresId;
						oltip=$('#oltip').val();
						$('#getoltip').textbox('setValue',data.dvip);
						$('#voipgw').val(data.dvgateway);
						$('#voiprange').val(data.iprange);
						alert(data.msg);
					}
				})
			}
		}
	});
	$('#step1').linkbutton({
		text:'下一步',
	    iconCls: 'icon-ok',
	    onClick:function(){
	    	if(!$('#st1').form('validate')){
	    		return;
	    	}
			oltname=$('#oltname').val();
			olttype=$('#olttype').combobox('getValue');
			oltpontype=$('#oltpontype').combobox('getValue');
			oltponnum=$('#oltponnum').val();
			boardnum=$('#boardnum').val();
			ylpon=$('#ylpon').val();
			zhukong=$('#zhukong').val();
			oltip=$('#getoltip').val();
			getgw=$('#getgw').val();
			iprange=$('#iprange').val();
			oltvoip=$('#getoltvoip').val();
			voipgw=$('#voipgw').val();
			voiprange=$('#voiprange').val();
			pppvlanbegin=$('#pppvlanbegin').val();
			pppvlanend=$('#pppvlanend').val();
			iptvvlan=$('#iptvvlan').val();
			ylvlan=$('#ylvlan').val();
			voipvlan=$('#voipvlan').val();
			var jfId=$('#jifang').combobox('getValue');
			var dvmodelId=$('#dvmodel').combobox('getValue');
			$.ajax({
				url:'addnewolt',
				type:'POST',
				data:{
					oltId:noltId,
					oltname:oltname,
					olttype:olttype,
					oltpontype:oltpontype,
					oltponnum:oltponnum,
					boardnum:boardnum,
					ylpon:ylpon,
					zhukong:zhukong,
					oltip:oltip,
					getgw:getgw,
					iprange:iprange,
					oltvoip:oltvoip,
					voipgw:voipgw,
					voiprange:voiprange,
					pppvlanbegin:pppvlanbegin,
					pppvlanend:pppvlanend,
					iptvvlan:iptvvlan,
					voipvlan:voipvlan,
					ylvlan:ylvlan,
					ipresId:ipresId,
					ipdeployId:ipdeployId,
					dvmodelId:dvmodelId,
					jfId:jfId,
				},
				success:function(data){
					alert(data.msg);
					return;
				}
			});
			
			
	    	$('#addparent').load('todvparent',{
	    		dvbelong:'olt',
	    		devresId:'1',
	    		dvip:$('#getoltip').val(),
	    		dvname:oltname,
	    		dvId:noltId
	    	},function(){
	    		$.getScript('/xasj/js/component/dvparent.js');
	    		$('#aa').accordion('select','设备上联');
	    		$('#dg').datagrid({
	    			width:'100%',
	    			height:'auto'
	    		});
	    		$('.datagrid-view').eq(0).after('<div><a id="pre1" style="width:80px"></a><a id="step2" style="width:100px"></a></div>');
	    		$('#pre1').linkbutton({
	    			text:'上一步',
	    		    onClick:function(){
	    		    	$('#aa').accordion('select','OLT参数设置');
	    		    }
	    		});
	    		$('#step2').linkbutton({
	    			text:'下一步',
	    			iconCls:'icon-ok',
	    			onClick:function(){
	    				oltponnum=$('#oltponnum').textbox('getValue');
	    				ylpon=$('#ylpon').numberbox('getValue');
	    				boardnum=$('#boardnum').numberbox('getValue');
	    				dvfirm=$('#dvfirm').combobox('getValue');
	    				oltId=$('#dvId').val();
	    				oltip=$('#getoltip').val();
	    				pppvlanbegin=$('#pppvlanbegin').val();
	    				pppvlanend=$('#pppvlanend').val();
	    				oltname=$('#oltname').val();
	    				zhukong=$('#zhukong').val();
	    				//alert(zhukong);
	    				$('#newonu').datagrid({
	    					iconCls: 'icon-edit',
	    					singleSelect: true,
	    					url:'shownewonu',
	    					width:'100%',
	    					height:'580px',
	    					onClickRow: onClickonuRow,
	    					rownumbers:true,
	    					queryParams:{
	    						oltponnum:oltponnum,
	    						ylpon:ylpon,
	    						boardnum:boardnum,
	    						dvfirm:dvfirm,
	    						oltId:oltId,
	    						oltip:oltip,
	    						pppvlanbegin:pppvlanbegin,
	    						pppvlanend:pppvlanend,
	    						zhukong:zhukong,
	    						oltname:oltname
	    					},
	    					toolbar: [{
	    						iconCls: 'icon-save',
	    						handler: function(){
	    							acceptonu();
	    							}
	    						},
	    						'-'
	    						,{
	    						iconCls: 'icon-remove',
	    						handler: function(){
	    							rejectonu();
	    							}
	    						}],
	    					columns:[[{
	    						field:'onustatus',width:'7%',title:'状态',editor:{
	    							type:'combobox',
	    							options:{
	    								valueField:'statusvalue',
	    								textField:'status',
	    								data:[{
	    									statusvalue:'未分配',
	    									status:'未分配'
	    								},{
	    									statusvalue:'预留',
	    									status:'预留'
	    								}]
	    							}
	    						}
	    					},{
	    						field:'onuaddress',width:'7%',title:'地址',editor:{
	    							editable:false	    							
	    						}
	    					},{
	    						field:'onuepbaloc',width:'7%',title:'EPBA板',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onupon',width:'7%',title:'PON口',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onuobd',width:'7%',title:'OBD位置',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onupppv',width:'8%',title:'pppoe vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'iptvvlan',width:'8%',title:'iptv vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'voipvlan',width:'8%',title:'voip vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'ylvlan',width:'8%',title:'预留 vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'pondetail',width:'8%',title:'ONU PON',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onuodf',width:'8%',title:'ONU ODF',editor:{
	    							editable:false
	    						}
	    					}]],
	    					onLoadSuccess:function(){
	    						$('#aa').accordion('select','配置ONU');
	    						//$('#newonu').after('<a id="step3" style="width:100px"></a>');
	    						//$(this).datagrid('freezeRow',0);
	    						$('#compl').show();
	    					}
	    				});
	    			}
	    		});
	    	});
	    }
	});
	$('#step3').linkbutton({
		text:'完成',
	    iconCls: 'icon-ok',
//	    onClick:function(){
//	    	alert('OK')
//	    }
	});
	$('#pre2').linkbutton({
		text:'上一步',
	    onClick:function(){
	    	$('#aa').accordion('select','设备上联');
	    }
	});
	$('#compl').hide();
	var editIndex1 = undefined;	
	function endEditing1(){
		if (editIndex1 == undefined){return true}
		if ($('#newonu').datagrid('validateRow', editIndex1)){
			//var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
			//var productname = $(ed.target).combobox('getText');
			//$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;
			//$('#dg').datagrid('endEdit', editIndex);
			editIndex1 = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickonuRow(index){
		if (editIndex1!= index){
			if (endEditing1()){
				$('#newonu').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex1 = index;
			} else {
				$('#newonu').datagrid('selectRow', editIndex1);
			}
		}
	}
	function removeitonu(){
		if (editIndex1 == undefined){return}
		$('#newonu').datagrid('cancelEdit', editIndex1)
				.datagrid('deleteRow', editIndex1);
		editIndex1 = undefined;
	}

	function acceptonu(){
		if (endEditing1()){
			$('#newonu').datagrid('acceptChanges');
		}
	}

	function rejectonu(){
		$('#newonu').datagrid('rejectChanges');
		editIndex1 = undefined;
	}
	
	$('#step3').click(function(){
		oltname=$('#oltname').val();
		olttype=$('#olttype').combobox('getValue');
		oltpontype=$('#oltpontype').combobox('getValue');
		oltponnum=$('#oltponnum').val();
		boardnum=$('#boardnum').val();
		ylpon=$('#ylpon').val();
		zhukong=$('#zhukong').val();
		oltip=$('#getoltip').val();
		getgw=$('#getgw').val();
		iprange=$('#iprange').val();
		oltvoip=$('#getoltvoip').val();
		voipgw=$('#voipgw').val();
		voiprange=$('#voiprange').val();
		pppvlanbegin=$('#pppvlanbegin').val();
		pppvlanend=$('#pppvlanend').val();
		iptvvlan=$('#iptvvlan').val();
		ylvlan=$('#ylvlan').val();
		voipvlan=$('#voipvlan').val();
		var jfId=$('#jifang').combobox('getValue');
		var dvmodelId=$('#dvmodel').combobox('getValue');
		var dvparent=$('#dg').datagrid('getData');
		var onulist=$('#newonu').datagrid('getData');
		$.ajax({
			url:'addnewolt',
			type:'POST',
			data:{
				oltId:noltId,
				oltname:oltname,
				olttype:olttype,
				oltpontype:oltpontype,
				oltponnum:oltponnum,
				boardnum:boardnum,
				ylpon:ylpon,
				zhukong:zhukong,
				oltip:oltip,
				getgw:getgw,
				iprange:iprange,
				oltvoip:oltvoip,
				voipgw:voipgw,
				voiprange:voiprange,
				pppvlanbegin:pppvlanbegin,
				pppvlanend:pppvlanend,
				iptvvlan:iptvvlan,
				voipvlan:voipvlan,
				ylvlan:ylvlan,
				ipresId:ipresId,
				ipdeployId:ipdeployId,
				dvmodelId:dvmodelId,
				jfId:jfId,
				
				dvparent:JSON.stringify(dvparent),
				onulist:JSON.stringify(onulist)	
			},
			success:function(data){
				alert(data.msg);
			}
		});
		
	});
	
    $.extend($.fn.validatebox.defaults.rules, {    
    	 ip : {// 验证IP地址  
             validator : function(value) {  
                 var reg = /^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$/ ;  
                 return reg.test(value);  
             	},  
             message : 'IP地址格式不正确'  
    	 	}
    });
});